/*
 * Copyright 2012 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.cell;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.BrowserEvents;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.PopupPanel;
import finbudgettable.client.ui.grid.CellGrid;

public class ContextMenuPopupPanel extends PopupPanel {

    public interface Resources extends ClientBundle {

        public interface Style extends CssResource {

            @ClassName("b-cell-context-menu")
            String bCellContextMenu();
        }

        @Source("ContextMenu.css")
        Style style();

    }

    public static final Resources RESOURCES = GWT.create(Resources.class);

    static {
        RESOURCES.style().ensureInjected();
    }

    private final CellGrid grid;

    public ContextMenuPopupPanel(CellGrid grid) {
        super(true, true);
        this.grid = grid;
    }

    @Override
    protected void onPreviewNativeEvent(Event.NativePreviewEvent event) {
        if (BrowserEvents.KEYUP.equals(event.getNativeEvent().getType())) {
            int keyCode = event.getNativeEvent().getKeyCode();
            if (keyCode == KeyCodes.KEY_ESCAPE) {
                hide();
                grid.setFocus(true);
            }
        }
    }
}
